<template>
  <a-modal
    title="新增产品型号"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="型号编码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="型号编码" v-decorator.trim="['code', {rules: [{required: true, message: '请输入型号编码！'}]}]" />
        </a-form-item>
        <a-form-item
          label="型号名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入型号名称" v-decorator="['name', {rules: [{required: true, message: '请输入型号名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="产品名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select style="width: 100%" placeholder="请选择产品名称" v-decorator="['proId', {rules: [{ required: true, message: '请选择产品名称！' }]}]">
            <a-select-option v-for="(item,index) in list.proModeList" :key="index" :value="item.id">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="库存单位"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select style="width: 100%" placeholder="请选择库存单位" v-decorator="['unit', {rules: [{ required: true, message: '请选择库存单位！' }]}]">
            <a-select-option v-for="(item,index) in unitData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="请输入备注" :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { proModelAdd } from '@/api/modular/main/promodel/proModelManage'
  import { proList } from '@/api/modular/main/pro/proManage'
  import XEUtils from 'xe-utils'
  export default {
    data () {
      return {
        queryParam: {
          proId: []
        },
        list: {
          proModeList: []
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        unitData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      add (queryParam) {
        this.visible = true
        const unitOption = this.$options
        this.queryParam = queryParam
        this.unitData = unitOption.filters['dictData']('unit')
        proList().then(res => {
          this.list.proModeList = res.data
        })
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              proId: queryParam.proId
            }
          )
        }, 100)
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.code = XEUtils.trim(values.code)
            values.name = XEUtils.trim(values.name)
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            proModelAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.message)// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
